//
//  IndexViewController.m
//  MY_IGList_demo
//
//  Created by mac on 2019/3/28.
//  Copyright © 2019 Yilian. All rights reserved.
//

#import "IndexViewController.h"
#import "PersonSectionController.h"
#import "PersonModel.h"
@interface IndexViewController ()<IGListAdapterDataSource,UIScrollViewDelegate>
{
    NSMutableArray *arr;
}
@property (nonatomic,strong) IGListAdapter *adapter;
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIActivityIndicatorView * activityIndicator;
@end

@implementation IndexViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.navigationController.navigationBar setHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    
    self.activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
    [self.view addSubview:self.activityIndicator];
    //设置小菊花的frame
    self.activityIndicator.frame= self.view.bounds;
    //设置小菊花颜色
//    self.activityIndicator.color = [UIColor orangeColor];
    //设置背景颜色
    self.activityIndicator.backgroundColor = [UIColor clearColor];
    //刚进入这个界面会显示控件，并且停止旋转也会显示，只是没有在转动而已，没有设置或者设置为YES的时候，刚进入页面不会显示
    self.activityIndicator.hidesWhenStopped = YES;
    [self requestData];
}

- (void)setUpCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;//设置每一行之间的间距
    layout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:self.collectionView];
}

- (NSArray<id <IGListDiffable>> *)objectsForListAdapter:(IGListAdapter *)listAdapter{
    
//    [arr addObject:[PersonModel new]];
    
    NSLog(@"%@",arr);
    return  arr;
}

- (IGListSectionController *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object{
    if ([object isKindOfClass:[PersonModel class]]) {
        return [PersonSectionController new];
    }else{
        return nil;
    }
}

- (nullable UIView *)emptyViewForListAdapter:(IGListAdapter *)listAdapter{
    return nil;
}


#pragma mark -- http_request
- (void)requestData{
    [self.activityIndicator startAnimating];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //设置请求数据类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    //设置返回数据类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
//    NSString *lng = [NSString stringWithFormat:@"%f",self.firstPlacemark.location.coordinate.longitude];
//    NSString *lat = [NSString stringWithFormat:@"%f",self.firstPlacemark.location.coordinate.latitude];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain",@"text/html", nil];
    
    NSDictionary *dic = @{
                          @"act":@"live_home",
                          @"op":@"seach",
                          @"class_id":@"2802",
                          @"lng":@"",
                          @"lat":@"",
                          @"curpage":@(1)
                          };
    NSString *URL = [NSString stringWithFormat:@"https://www.beanlife123.com/app/index.php"];
    [manager POST:URL parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"获取首页数据:%@",responseObject);
        arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"datas"][@"storeList"]) {
            PersonModel *model = [PersonModel new];
            model.name = dic[@"chain_name"];
            [arr addObject:model];
        }
    
       [self setUpCollectionView];
        //初始化IGListAdapter
        self.adapter = [[IGListAdapter alloc]initWithUpdater:[IGListAdapterUpdater new] viewController:self workingRangeSize:1];
        //关联collectionView
        self.adapter.collectionView = self.collectionView;
        //设置dataSource,实现3个代理方法
        self.adapter.dataSource = self;
        [self.activityIndicator stopAnimating];
        
//        [self.adapter.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    [self.activityIndicator stopAnimating];
        NSLog(@"---------%@",error);
    }];
}

@end
